<?php 

	function adsl_dial(){
		echo "dialing....\n";
		$adsl_connection = iconv("utf-8", "gbk", ADSL_CONNECTION);
		$str = "rasdial \"$adsl_connection\" ".ADSL_USERNAME." ".ADSL_PASSWORD; 
		//echo $str;
		exec($str, $data, $ret);
		//var_dump($data);
		return $ret;
	}
	
	function adsl_disconnect(){
		$str = "rasdial /DISCONNECT"; 
		exec($str, $data, $ret);
		//var_dump($data);
		return  $ret;
	}
	
	function adsl_redial()
	{
		adsl_disconnect();
		adsl_dial();
	}
	
	function sendMailEx($subject, $content='', $attach_list=array(), $toBoss=0)
	{
		global $global_smtp_maillist_debug, $global_smtp_maillist_boss;
		$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
		$mail->IsSMTP(); // telling the class to use SMTP
		try {
			$mail->Host       = SMTP_HOST; // SMTP server
			$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
			$mail->SMTPAuth   = true;                  // enable SMTP authentication
			$mail->CharSet = "utf-8";
			//$mail->Port       = 26;                    // set the SMTP port for the GMAIL server
			$mail->Username   = SMTP_USERNAME; // SMTP account username
			$mail->Password   = SMTP_PASSWORD;        // SMTP account password 
			$mail->SetFrom(SMTP_SENDER_EMAIL, SMTP_USERNAME);
			$mail->AddReplyTo(SMTP_SENDER_EMAIL, 'addReplyTo');
			//add maillist debug
			foreach ($global_smtp_maillist_debug as $address_debug){
				$mail->AddAddress($address_debug, $address_debug);  
			}
			//add maillist boss
			if ($toBoss) {
				foreach ($global_smtp_maillist_boss as $address_boss){
					$mail->AddAddress($address_boss, $address_boss);  
				}
			}
			
			$mail->Subject = $subject;
			$mail->AltBody = $subject; // optional - MsgHTML will create an alternate automatically
			$mail->MsgHTML($content);
			if (!empty($attach_list)) {
				foreach ($attach_list as $attachment){
					$mail->AddAttachment($attachment);      // attachment;
				}
			}
		   
			if($mail->Send()){
				printlog("Message Sent OK, subject is: $subject", "sendmail");
			}
			else {
				printlog("Message Sent FAIL, subject is: $subject", "sendmail");
			}
		} catch (phpmailerException $e) {
		  	printlog($e->errorMessage(),"sendmail"); //Pretty error messages from PHPMailer
		} catch (Exception $e) {
		  	printlog($e->getMessage(), "sendmail"); //Boring error messages from anything else!
		}
	}

	//获得请求resouce相关的东西，返回handle
	function makeRequestResource($url ,$method="GET", $parameter=array(), $dumpCookie=true, $dumpcookieFile="", $cookieFile="" , $opt_arr = array())
	{
		$isPost = false;
		if(substr($url,0,7) != 'http://' && substr($url, 0, 8) != "https://" )
		{
			$url = 'http://'.$url;
		}
		$handle = curl_init($url);
		if($handle === false){
			printlog("init error,init url is :".$url."\n");
			printlog(curl_error($handle)."\n");
			//exit();
			return false;
		}
		//https
		if (substr($url, 0, 8) == "https://") {
			curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, 2);
			//curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, 1);
			curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);
			//curl_setopt($handle, CURLOPT_CAPATH, ROOT_PATH."/include/cacert.pem");
			//curl_setopt($handle, CURLOPT_CAINFO, ROOT_PATH."/include/cert.cer");
		}
		
		curl_setopt($handle, CURLOPT_VERBOSE, "true");
		curl_setopt($handle, CURLOPT_TIMEOUT, 20);
		curl_setopt($handle, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0');
		if(strcasecmp($method, 'POST') == 0 ){
			curl_setopt($handle, CURLOPT_POST, true);
			$isPost = true;
		}
		if(!empty($cookieFile)){
			curl_setopt($handle, CURLOPT_COOKIEFILE, $cookieFile);
			curl_setopt($handle, CURLOPT_COOKIEJAR, $cookieFile);
		}
		//var_dump($cookieFile);exit;
		if(!empty($parameter)){
			if($isPost){
				$parameter = http_build_query($parameter);
				curl_setopt($handle, CURLOPT_POSTFIELDS, $parameter);
			}else{
				$str = ""; 
				foreach($parameter as $key=>$value){
					$str .= $key."=".urlencode($value)."&";
				}
				if(strpos($url, "?") === false){
					$url = $url."?";
				}
				$url = $url.$str;
				curl_setopt($handle, CURLOPT_URL, $url ); 
			}	
		}
		$ip = rand(1,255).'.'.rand(1,255).'.'.rand(1,255).'.'.rand(1,255);
		$headers = array('Expect:','X-FORWARDED-FOR:'.$ip,'CLIENT-IP:'.$ip);
		curl_setopt($handle,CURLOPT_HTTPHEADER, $headers);
		if(!empty($opt_arr)){
			foreach ((array)$opt_arr as $opt => $value){
				curl_setopt($handle, $opt, $value);
			}
		}
		//curl_setopt($handle, CURLOPT_HEADER, false);
		curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
		
		if($dumpCookie){
			curl_setopt($handle, CURLOPT_COOKIEJAR, $dumpcookieFile);
		}

		return $handle;
		
	}
	
	/**
	 *  执行一个请求，返回结果页面
	 *
	 * */
	function makeRequest($url ,$method="GET", $parameter=array(), $dumpCookie=true, $dumpcookieFile="", $cookieFile="", $opt_arr=array() )
	{
		//$handle = makeRequestResource($url, $method, $parameter, $dumpCookie, $dumpcookieFile, $cookieFile, "http://search.zhenai.com/search");
		$handle = makeRequestResource($url, $method, $parameter, $dumpCookie, $dumpcookieFile, $cookieFile, $opt_arr);
		$ret = curl_exec($handle);
		if($ret === false)
		{
			printlog("exec error", "warning");
			printlog("url is :$url", "warning");
			printlog(curl_error($handle), "warning");
			return false;
		}
		curl_close($handle); 
		return $ret;
	}
	
	//工具方法，使用指定的cookie文件来请求
	function execRequest($url, $method, $parameter, $cookieFile, $referer)
	{
		return makeRequest($url, $method, $parameter, false, '', $cookieFile, $referer);
	}
	
	
	
	if(!function_exists('file_get_contents') ){
		function file_get_contents($file){
			$f = fopen($file, 'r');
			$str = '';
			if(!$f){
				return $str;
			} 
			while(!feof($f)){
				$temp = fread($f, filesize($f));
				$str .= $temp;
			}
			fclose($f);
			return $str;
		}
	}
	if(!function_exists('file_put_contents')){
		function file_put_contents($file, $data){
			$f = fopen($file, 'w');
			if(!fwrite($f, $data, strlen($data))){
				echo "write $data to $file failed\n";
				return false;
			}
			fclose($f);
			return true;
		}
	}
	
	function printlog($content, $level='debug')
	{
		global $global_site;
		$logfile = ROOT_PATH."/".$global_site."/log/".date("Ymd")."_".$level.".txt";
		if (is_object($content) || is_array($content)) {
			echo date("Ymd H:i:s")."----";var_dump($content);echo "\n";
			file_put_contents($logfile, date("Ymd H:i:s")."----".serialize($content)."\n", FILE_APPEND);
		}
		else{
			echo date("Ymd H:i:s")."----".$content."\n";
			file_put_contents($logfile, date("Ymd H:i:s")."----".$content."\n", FILE_APPEND);
		}
	}
	
	/**
	 * 判断一个页面是否抓取完全，通过判断是否有</html>标签
	 * @param $html
	 */
	function checkCompleteDownload($page)
	{
		if (strpos($page, "</html>") === false ) {
			return false;
		}
		else{
			return true;
		}
	}
	
	//base function: used for extracting
	function simpleExtract($pattern, $page, $field)
	{
		$arr = array();
		$page = str_replace(array(" ","\t","\n","\r"), "", $page);
		printlog($page);
		preg_match_all($pattern, $page, $arr, PREG_SET_ORDER);
		//if($field=="email"){var_dump($arr);exit;}
		if(isset($arr[0][1])){
			$val = $arr[0][1];
			//printlog("$field : $val ");
			return $val;
		}
		else{
			global $global_site;
			//百合网可以忽略has_photo字段提取失败的警报
			if(!($global_site == "baihe" && in_array($field, array("has_photo")))){
				printlog("$field extract error: $pattern page: $page", "extract_error");
			}
			return "";
		}
	}
	
	//获取某个日期对应的表名
	function getTblName($ymd="")
	{
		if (empty($ymd)) {
			$ym = date("Ym", time()-60*24*24);
		}
		else{
			$ym = date("Ym", strtotime($ymd));
		}
		return "competitor_".$ym;
	}
	
	//统计某个日期抓取的数据
	function sendDataInfoMail($site, $ymd="")
	{
		if (empty($ymd)) {
			$ymd = date("Y-m-d", time()-60*24*24);
		}
	
		global $conn;
		$title = $body = "";
		$tblName = getTblName($ymd);
		//抓取总数
		$sql = "select count(*) as cnt from $tblName where `from`='$site' and fetch_date='$ymd' ";
		$cnt = $conn->result($sql);
		
		//按性别
		$sql = "select count(*) as cnt from $tblName where `from`='$site' and fetch_date='$ymd' and gender='1'";
		$cnt_gg = $conn->result($sql);
		$sql = "select count(*) as cnt from $tblName where `from`='$site' and fetch_date='$ymd' and gender='0'";
		$cnt_mm = $conn->result($sql);
		$sql = "select count(*) as cnt from $tblName where `from`='$site' and fetch_date='$ymd' and gender<>'0' and gender<>'1'";
		$cnt_nosex = $conn->result($sql);
		
		//按有无照片
		$sql = "select count(*) as cnt from $tblName where `from`='$site' and fetch_date='$ymd' and has_photo=1";
		$cnt_photo = $conn->result($sql);
		$sql = "select count(*) as cnt from $tblName where `from`='$site' and fetch_date='$ymd' and has_photo=0";
		$cnt_nophoto = $conn->result($sql);
		
		//按年龄
		$sql = "select count(*) as cnt from $tblName where `from`='$site' and fetch_date='$ymd' and age<20";
		$cnt_10 = $conn->result($sql);
		$sql = "select count(*) as cnt from $tblName where `from`='$site' and fetch_date='$ymd' and age>=20 and age<30";
		$cnt_20 = $conn->result($sql);
		$sql = "select count(*) as cnt from $tblName where `from`='$site' and fetch_date='$ymd' and age>=30 and age<40";
		$cnt_30 = $conn->result($sql);
		$sql = "select count(*) as cnt from $tblName where `from`='$site' and fetch_date='$ymd' and age>=40 and age<50";
		$cnt_40 = $conn->result($sql);
		$sql = "select count(*) as cnt from $tblName where `from`='$site' and fetch_date='$ymd' and age>=50 and age<60";
		$cnt_50 = $conn->result($sql);
		$sql = "select count(*) as cnt from $tblName where `from`='$site' and fetch_date='$ymd' and age>=60";
		$cnt_60 = $conn->result($sql);
		$cnt_age = $cnt_10 + $cnt_20 + $cnt_30 + $cnt_40 + $cnt_50 + $cnt_60;
		
		//省份
		$sql = " select count(*) as cnt from $tblName where `from`='$site' and fetch_date='$ymd' and province='' ";
		$cnt_noprov = $conn->result($sql);
		$sql = " select count(*) as cnt from $tblName where `from`='$site' and fetch_date='$ymd' and province<>'' ";
		$cnt_hasprov = $conn->result($sql);
		$arr_prov = array();
		$sql = " select province, count(province) as cnt from $tblName where `from`='$site' and fetch_date='$ymd' group by province order by cnt desc ";
		$query = $conn->query($sql);
		while($row = $conn->fetch_array()){
			$province = $row["province"];
			if(empty($province)){
				$province = "no value";
			}
			$arr_prov[$province] = $row["cnt"];
		}
		//发送邮件
		$title = "本次  $site 抓取用户数为  $cnt ";
		$body = "男用户  $cnt_gg 人，女用户  $cnt_mm 人， 无性别用户  $cnt_nosex 人 <br>"
				."有照片 $cnt_photo 人，无照片 $cnt_nophoto 人 <br>"
				."年龄: 10+ $cnt_10 人， 20+ $cnt_20 人， 30+ $cnt_30 人， 40+ $cnt_40 人， 50+ $cnt_50 人， 60+ $cnt_60 人 ， 总计 $cnt_age 人 <br>"
				."有省份信息的 $cnt_hasprov 人， 无省份信息的 $cnt_noprov 人 <br><br>"
				."省份排名: <br>";
		foreach($arr_prov as $prov => $cnt){
			$body .= $prov."\t".$cnt."<br>";
		}
				
		
		sendMailEx($title, $body);
	}
	
	//将某一天的数据导出成sql文件
	function dataDump($site, $ymd="")
	{
		if (empty($ymd)) {
			$ymd = date("Y-m-d", time()-60*24*24);
		}
	
		global $conn,$dataDir;
		$datafile = ROOT_PATH."/".$site."/data/".$site."_".$ymd.".sql";
		file_put_contents($datafile, ""); //清空
		$tblName = getTblName($ymd);
		$sql = "select * from $tblName where `from`='$site' and fetch_date='$ymd' ";
		$query = $conn->query($sql);
		while($row = $conn->fetch_array()){
			$id = $row['id'];
			$city = $row['city'];
			$province = $row['province'];
			$age = $row['age'];
			$gender = $row['gender'];
			$has_photo = $row['has_photo'];
			$arr = array("id" => null,
						"fetch_date" => $ymd,
						"`from`" => $site,
						"source_user_id" => $id,
						"age" => $age,
						"province" => $province,
						"city" => $city,
						"gender" => $gender,
						"has_photo" => $has_photo,
						);		
			$sql_insert = Sql::insert($arr, $tblName);
			//echo $sql_insert;exit;
			file_put_contents($datafile, $sql_insert."; \n", FILE_APPEND);
		}
		return $datafile;
	}
	
	//将某个文件上传至ftp服务器
	function dataUpload($datafile)
	{
		if(!$handle = fopen($datafile, 'rb')){
			printlog("error opening ".$datafile, "ftp_upload");
		}
		
		$size = filesize($datafile);
		$contents = file_get_contents($datafile);
		$file_base_name = basename($datafile);
		while(true){
			$remote_file = "ftp://".FTP_USERNAME.":".FTP_PASSWORD."@".FTP_SERVER.":".FTP_PORT.FTP_ROMATE_PATH.$file_base_name;
			//echo $remote_file;exit;
			$len = file_put_contents($remote_file, $contents);
			if($len == $size){
				$title = "$datafile 上传到ftp成功， 上传文件大小为 $size ";
				sendMailEx($title, $title);
				break;
			}
			else{
				printlog("upload $datafile error,retry....", "ftp_upload");
				unlink($remote_file);
			}
		}
	}
	
	//获取当前的外网ip
	function getip() 
	{
		$url = "http://www.ip138.com/ip2city.asp";
		$page = makeRequest($url, "", "", false, "", "", "");
		preg_match('/\[(.*)\]/', $page, $ip);
		return $ip[1];
	}
	//删除过期的pages的目录文件
	function deldir($dir) {
		//先删除目录下的文件：
		$dh=opendir($dir);
		while ($file=readdir($dh)) {

			if($file!="." && $file!="..") {
				$fullpath=$dir."/".$file;
				if(!is_dir($fullpath)) {
					unlink($fullpath);
				} else {
					deldir($fullpath);
				}
			}
		}
		closedir($dh);
		  //删除当前文件夹：
		  if(rmdir($dir)) {
			return true;
		  } else {
			return false;
		  }
	}

?>